package com.ruoyi.zsyh.controller.applet.store;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务订单 -- 消息
 */
@Controller
@RequestMapping("/applet/zsyh/serviceOrderMessage")
public class ZsyhServiceOrderMessageAppletController extends BaseController
{

    @Autowired
    private IZsyhUserRepairstoreService zsyhUserRepairstoreService;

    @Autowired
    private IZsyhServiceOrderMessageService zsyhServiceOrderMessageService;
    /**
     * 维修师傅 订单消息未读消息数量
     * @return
     */
    @RequestMapping("/getServiceOrderMessageNum")
    @ResponseBody
    public AjaxResult getServiceOrderMessageNum(String userId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();//师傅的店铺id

        //配件消息
        ZsyhServiceOrderMessage message = new ZsyhServiceOrderMessage();
        message.setIsRead("0");
        message.setUserId(repairId);
        message.setMessageType("2");
        List<ZsyhServiceOrderMessage> messageList2 = zsyhServiceOrderMessageService.selectZsyhServiceOrderMessageList(message);
        //费用消息
        message = new ZsyhServiceOrderMessage();
        message.setIsRead("0");
        message.setUserId(repairId);
        message.setMessageType("3");
        List<ZsyhServiceOrderMessage> messageList3 = zsyhServiceOrderMessageService.selectZsyhServiceOrderMessageList(message);
        //交易消息
        message = new ZsyhServiceOrderMessage();
        message.setIsRead("0");
        message.setUserId(repairId);
        message.setMessageType("4");
        List<ZsyhServiceOrderMessage> messageList4 = zsyhServiceOrderMessageService.selectZsyhServiceOrderMessageList(message);

        //系统消息
        message = new ZsyhServiceOrderMessage();
        message.setMessageType("1");
        Map map = new HashMap();
        map.put("repairId", repairId);
        map.put("repairNotRed", "repairNotRed");//查询未读消息
        message.setParams(map);
        List<ZsyhServiceOrderMessage> messageList1 = zsyhServiceOrderMessageService.selectZsyhServiceOrderMessageList(message);

        map = new HashedMap();
        map.put("messageListSize1", messageList1.size());
        map.put("messageListSize2", messageList2.size());
        map.put("messageListSize3", messageList3.size());
        map.put("messageListSize4", messageList4.size());

        return success("成功", map);
    }

    /**
     * 维修师傅 订单消息列表
     * @messageType 分类 1系统消息 2配件消息 3费用消息 4交易消息
     * @return
     */
    @RequestMapping("/getServiceOrderMessage")
    @ResponseBody
    public AjaxResult getServiceOrderMessage(String userId, String messageType, String pageNum, String pageSize)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();//师傅的店铺id

        ZsyhServiceOrderMessage message = new ZsyhServiceOrderMessage();
        if(!"1".equals(messageType)){
            message.setUserId(repairId);
        }
        message.setMessageType(messageType);
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        map.put("repairId", repairId);
        message.setParams(map);
        List<ZsyhServiceOrderMessage> messageList = zsyhServiceOrderMessageService.selectZsyhServiceOrderMessageList(message);

        return success("成功", messageList);
    }

    /**
     * 维修师傅 订单消息阅读
     * @return
     */
    @RequestMapping("/readMessage")
    @ResponseBody
    public AjaxResult readMessage(String messageId, String userId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String repairId = store.getId();//师傅的店铺id

        ZsyhServiceOrderMessage message = zsyhServiceOrderMessageService.selectZsyhServiceOrderMessageById(messageId);
        String messageType = message.getMessageType();//分类 1系统消息 2配件消息 3费用消息 4交易消息
        ZsyhServiceOrderMessage _message = new ZsyhServiceOrderMessage();
        _message.setId(messageId);
        if(!"1".equals(messageType)){
            _message.setIsRead("1");
        }else{
            _message.setReadUserId(message.getReadUserId() + repairId + ",");
        }
        zsyhServiceOrderMessageService.updateZsyhServiceOrderMessage(_message);
        return success("成功");
    }

}
